package widget.rapid.bucuresti;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import widget.rapid.bucuresti.R;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.RemoteViews;

public class RapidActivity extends Activity implements OnClickListener {
	public static final int ANIMATION_DURATION = 700;

	private ImageView sampleImgView;

	private int selectedImg = R.drawable.rapid0;

	private AlphaAnimation infoAppearance = new AlphaAnimation(0, 1);

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.setTitle(R.string.activity_title);

		ImageView icon0 = (ImageView) findViewById(R.id.imageView0);
		ImageView icon1 = (ImageView) findViewById(R.id.imageView1);
		ImageView icon2 = (ImageView) findViewById(R.id.imageView2);
		ImageView icon3 = (ImageView) findViewById(R.id.imageView3);
		ImageView icon4 = (ImageView) findViewById(R.id.imageView4);
		ImageView icon5 = (ImageView) findViewById(R.id.imageView5);
		ImageView icon6 = (ImageView) findViewById(R.id.imageView6);
		ImageView icon7 = (ImageView) findViewById(R.id.imageView7);
		ImageView icon8 = (ImageView) findViewById(R.id.imageView8);
		ImageView icon9 = (ImageView) findViewById(R.id.imageView9);
		sampleImgView = (ImageView) findViewById(R.id.sampleImageView);

		icon0.setOnClickListener(this);
		icon1.setOnClickListener(this);
		icon2.setOnClickListener(this);
		icon3.setOnClickListener(this);
		icon4.setOnClickListener(this);
		icon5.setOnClickListener(this);
		icon6.setOnClickListener(this);
		icon7.setOnClickListener(this);
		icon8.setOnClickListener(this);
		icon9.setOnClickListener(this);
		sampleImgView.setOnClickListener(this);

		infoAppearance.setDuration(ANIMATION_DURATION);

		try {
			// Ads section
			AdView adView = (AdView) findViewById(R.id.adView1);
			AdRequest ar = new AdRequest();
			ar.setGender(AdRequest.Gender.MALE);
			adView.loadAd(ar);
		} catch (Exception e) {
			Log.e("BatteryInfo",
					"There was an excepion when trying to load the Ads", e);
			e.printStackTrace();
		}
	}

	private void updateWidgets(int resId) {
		AppWidgetManager appmanager = AppWidgetManager.getInstance(this);
		ComponentName cmpName = new ComponentName(this, RapidWidget.class);
		int[] widgetIds = appmanager.getAppWidgetIds(cmpName);

		updateWidgets(this, appmanager, widgetIds, resId);
	}

	private void updateWidgets(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds, int resId) {
		for (int wid : appWidgetIds) {
			SharedPreferences prefs = Prefs.get(context);
			Editor editor = prefs.edit();

			editor.putInt(String.valueOf(wid), resId);
			editor.commit();
			RemoteViews rView = new RemoteViews(context.getPackageName(),
					R.layout.widget);
			rView.setImageViewResource(R.id.widgetImageView, resId);

			appWidgetManager.updateAppWidget(wid, rView);
		}
	}

	private void updateCurrentWidget(int resId) {
		AppWidgetManager appmanager = AppWidgetManager.getInstance(this);

		RemoteViews rView = new RemoteViews(this.getPackageName(),
				R.layout.widget);
		rView.setImageViewResource(R.id.widgetImageView, resId);
		int wid = this.getIntent().getIntExtra(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		if (wid != AppWidgetManager.INVALID_APPWIDGET_ID) {
			SharedPreferences prefs = Prefs.get(this);
			Editor editor = prefs.edit();

			editor.putInt(String.valueOf(wid), resId);
			editor.commit();
			appmanager.updateAppWidget(wid, rView);
		} else {
			// update all widgets
			updateWidgets(resId);
		}
	}

	@Override
	public void onClick(View v) {
		int drawableID = 0;

		switch (v.getId()) {
		case R.id.imageView0:
			drawableID = R.drawable.rapid0;
			break;

		case R.id.imageView1:
			drawableID = R.drawable.rapid1;
			break;

		case R.id.imageView2:
			drawableID = R.drawable.rapid2;
			break;

		case R.id.imageView3:
			drawableID = R.drawable.rapid3;
			break;

		case R.id.imageView4:
			drawableID = R.drawable.rapid4;
			break;

		case R.id.imageView5:
			drawableID = R.drawable.rapid5;
			break;

		case R.id.imageView6:
			drawableID = R.drawable.rapid6;
			break;

		case R.id.imageView7:
			drawableID = R.drawable.rapid7;
			break;

		case R.id.imageView8:
			drawableID = R.drawable.rapid8;
			break;

		case R.id.imageView9:
			drawableID = R.drawable.sapunaru;
			break;

		case R.id.sampleImageView:
			updateCurrentWidget(selectedImg);
			// finish this activity
			this.finish();
			break;
		}

		if (drawableID != 0) {
			sampleImgView.setImageResource(drawableID);
			sampleImgView.startAnimation(infoAppearance);
			selectedImg = drawableID;
		}
	}
}